#include "virtualpaintengine.h"
#include <QDebug>
#include <QPainter>
#include <QPen>
#include <QBrush>

#define YOFFSET 20
#define XOFFSET 20

VirtualPaintEngine::VirtualPaintEngine()
{
    window_ = new Screen(this);
    window_->setGeometry(400, 400, 500, 300);
    window_->setWindowTitle("虚拟LCD");
    window_->show();

    timer_ = new QTimer(this);
    connect(timer_, SIGNAL(timeout()), this, SLOT(paint()));
    timer_->setInterval(500);
    timer_->start();
}

VirtualPaintEngine::~VirtualPaintEngine()
{
    timer_->stop();
    delete timer_;
    timer_ = NULL;

    delete window_;
    window_ = NULL;
}

int VirtualPaintEngine::writeText(QRect rect, const QString &text, Color fontColor, Color backColor)
{
    QColor font_color(fontColor.r, fontColor.g, fontColor.b);
    QColor back_color(backColor.r, backColor.g, backColor.b);

    qDebug() << "Paint <" << text << ">, rect" << rect;
    int x = rect.x(), y = rect.y();
    if(x>=SCREEN_WIDTH||x<0||y>=SCREEN_HEIGHT||y<0){
        qWarning() << Q_FUNC_INFO << "Position out of range, x:"<< x <<"y:" << y;
        return false;
    }

    int maxX = qMin(SCREEN_WIDTH, x+text.length());
    int i = 0;
    int xx;
    for(xx=x; xx<maxX && xx<SCREEN_WIDTH; xx++){
        frame_[y][xx] = text[i++];
        font_color_[y][xx] = font_color;
        back_color_[y][xx] = back_color;

        if(frame_[y][xx].unicode() > 128){   // left one space
            frame_[y][++xx] = ' ';
            font_color_[y][xx] = font_color;
            back_color_[y][xx] = back_color;
            maxX += 1;
        }
    }
    while(xx<x+rect.width()&&xx<SCREEN_WIDTH){
        frame_[y][xx++] = ' ';
    }

    return 0;
}

int VirtualPaintEngine::clearRect(QRect rect)
{
    int x = rect.x(), y = rect.y(), width = rect.width(), height = rect.height();
    int maxX = qMin(x+width, SCREEN_WIDTH);
    int maxY = qMin(y+height, SCREEN_HEIGHT);

    for(int yy=y; yy<maxY; ++yy){
        for(int xx = x; xx<maxX; ++xx){
//            if(frame_[yy][xx].unicode() > 127)
//                maxX = qMin(maxX+1, SCREEN_WIDTH);

            frame_[yy][xx] = QChar(' ');
            font_color_[yy][xx] = QColor(0, 0, 0);
            back_color_[yy][xx] = QColor(255, 255, 255);
        }
    }
    return 0;
}

int VirtualPaintEngine::clear()
{
    clearRect(QRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT));
    return 0;
}

void VirtualPaintEngine::paint()
{
    window_->update();
}

Screen::Screen(VirtualPaintEngine *engine)
    :QWidget()
    ,engine_(engine)
{

}

void Screen::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QFont font;

    font.setPixelSize(15);
    font.setFamily("Courier");  // monospace font, same width.
    painter.setFont(font);
    int y=0;
    int offset = XOFFSET;
    for(int line=0; line<SCREEN_HEIGHT; ++line){
        for(int index=0; index<SCREEN_WIDTH; ++index){
            painter.setPen(engine_->font_color_[line][index]);
            painter.drawText(offset, y*15+YOFFSET, engine_->frame_[line][index]);
            offset += 8;
        }
        y++;
        offset = XOFFSET;
    }
}

